
window.com = {};

(function(){

    this.delDom = function(arr){
        window.p = {};
        $.each(arr,function(index,item){
            $(item).remove();
        })
    };
    this.init = function(){
      if(location.href.indexOf('#')<0){
          //location.href = "/"+para.path+"#processed";
      }
    };
    this.getEnv = function(){
        return this.getCookie('env') ? this.getCookie('env') : ''
    };
    this.textFormat = function(content){
        var code = encodeURIComponent(content);
        return decodeURIComponent(code.replace(/%0/g,'<br/>'));
    };
    this.layerInsert = function(dom,fun){
        /**局部页面注意不能绑定body document等事件 防止路由变换二次绑定**/
        var tmpBox = $("<div class='layer-box'></div>");
        function open(){
            tmpBox.fadeIn(200,function(){
                fun();
            });
        }
        function close(){
            tmpBox.fadeOut(200,function(){
                tmpBox.remove()
            });
        }
        tmpBox.on('click',".box-close",function(){
            close();
        })

        tmpBox.html(dom);
        $("body").append(tmpBox);
        open();
        return {
            open:open,
            close:close
        }
    };

    this.base64_encode = function(str){
        var c1, c2, c3;
        var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        var i = 0, len= str.length, string = '';

        while (i < len){
            c1 = str.charCodeAt(i++) & 0xff;
            if (i == len){
                string += base64EncodeChars.charAt(c1 >> 2);
                string += base64EncodeChars.charAt((c1 & 0x3) << 4);
                string += "==";
                break;
            }
            c2 = str.charCodeAt(i++);
            if (i == len){
                string += base64EncodeChars.charAt(c1 >> 2);
                string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                string += base64EncodeChars.charAt((c2 & 0xF) << 2);
                string += "=";
                break;
            }
            c3 = str.charCodeAt(i++);
            string += base64EncodeChars.charAt(c1 >> 2);
            string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
            string += base64EncodeChars.charAt(c3 & 0x3F)
        }
        return string
    };
    this.formatDate = function (strTime,type,type2) {
        var date = new Date(strTime ? strTime : new Date().getTime());
        var y = date.getFullYear();
        var m = date.getMonth()+1;
        var d = date.getDate();
        var h = date.getHours();
        var f = date.getMinutes();
        var s = date.getSeconds();
        var arr = [m,d,h,f,s];
        arr.forEach(function(val,index,self){
            if(val.toString().length<=1){
                self[index] = '0' + val;
            }
        });
        return y+type+arr[0]+type+arr[1]+" "+arr[2]+type2+arr[3]+type2+arr[4]
    };

    this.setCookie = function(name,value,hours){
        var expdate = new Date();
        expdate.setTime(expdate.getTime()+hours*3600*1000);
        document.cookie = name+'='+encodeURIComponent(value)+';expires='+expdate.toUTCString()+";path=/";
    };

    this.getCookie = function(c_name){
        if(document.cookie.length>0){
            var arrstr = document.cookie.split("; ");
            for(var i = 0;i<arrstr.length;i++){
                var temp = arrstr[i].split("=");
                if(temp[0] == c_name) {return decodeURIComponent(temp[1]); }
            }
        }
    };

    this.setData = function(type,sign,hours){
        localStorage.removeItem(type);
        var signObj = sign;
        //设置过期时间
        var time = new Date().getTime()+ hours*60*60*1000;
        signObj.expire = time;
        localStorage.setItem(type,JSON.stringify(signObj));
    };
    this.getData = function(type,key){
        var data = JSON.parse(localStorage.getItem(type)||localStorage.getItem(type));
        if(data){
            var d = new Date().getTime();
            if(d>data['expire']){
                return false;
            }else{
                return data[key];
            }
        }
    };

}).apply(com);